package com.pda.yuanjing.pages.inbilln.scanf;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.BeanNOut;
import com.pda.yuanjing.message.MessageBus;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ScanfAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;
    List<BeanNOut.DataBean> mlists = new ArrayList<>();
    private int selectedPosition = -1;// 选中的位置

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public ScanfAdapter(Context context, List<BeanNOut.DataBean> lists) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_inscanf, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.checkbox.setChecked(mlists.get(position).isCk());
        viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageBus messageBus = new MessageBus();
                messageBus.setIndex(position);
                if(mlists.get(position).isCk()){
                    messageBus.setValue(0);
                }else {
                    messageBus.setValue(1);
                }
                EventBus.getDefault().post(messageBus);
            }
        });
        viewHolder.tv1.setText( mlists.get(position).getProductName());
        viewHolder.tv2.setText("应扫:" + mlists.get(position).getYnum());
        viewHolder.tv3.setText("实扫:" + mlists.get(position).getSnum());
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.checkbox)
        CheckBox checkbox;
        @BindView(R.id.tv1)
        TextView tv1;
        @BindView(R.id.tv2)
        TextView tv2;
        @BindView(R.id.tv3)
        TextView tv3;
        @BindView(R.id.rl)
        RelativeLayout rl;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

